﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Utility;
using Newtonsoft.Json;
using Services.IService;
using Models;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace JFnetcore.Controllers
{
    public class APIController : Controller
    {
        private IProductService _productService;
        private IProducttypeService _producttypeService;
        private ILogService _logService;
        public APIController(IProductService productService, ILogService logService, IProducttypeService producttypeService)
        {
            _productService = productService;
            _producttypeService = producttypeService;
            _logService = logService;
        }

        #region 外部API接口
        /// <summary>
        /// 提供外部接口
        /// </summary>
        /// <param name="productJsn">{"product_name":"软件名称","product_No":"软件编号","product_txt":"软件描述","product_url":"下载地址","productType_Id":1}</param>
        /// <param name="key">区分大小写</param>
        /// <returns></returns>
        [Route("API/PostProduct/{productJsn?}/{key?}")]
        public IActionResult PostProduct(string productJsn, string key)
        {
            if (key != "JinFan")
            {
                return Content(ReturnJsonHelp.OutHandlerJson(false, "提交失败,key错误！"));
            }
            else
            {
                try
                {
                    ProductModel model = JsonConvert.DeserializeObject<ProductModel>(productJsn);
                    model.Id = 0;//都添加
                    model.Createtime = DateTime.Now;
                    model.Disable = 1;//默认启用
                    if (_productService.Insert(model)>0)
                    {
                        return Content(ReturnJsonHelp.OutHandlerJson(true, "提交成功！"));
                    }
                    else
                    {
                        return Content(ReturnJsonHelp.OutHandlerJson(false, "提交失败！"));
                    }
                }
                catch (Exception e)
                {
                    return Content(ReturnJsonHelp.OutHandlerJson(false, "提交失败," + e.Message + "    例:[{\"product_name\":\"软件名称\",\"product_No\":\"软件编号\",\"product_txt\":\"软件描述\",\"product_url\":\"下载地址\",\"productType_Id\":1}]"));
                }
            }
        }
        #endregion
        
    }
}
